﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TN_CMS.Domain.Entities;

namespace TN_CMS.Domain.Concrete
{
    public class EFDbContext : DbContext
    {
        public DbSet<SysAdmin> SysAdmins { get; set; }
        public DbSet<SysRole> SysRoles { get; set; }
        public DbSet<SysFeature> SysFeatures { get; set; }
        public DbSet<Product> Products { get; set; }
        public DbSet<News> News { get; set; }
        public DbSet<NewsGroup> NewsGroups { get; set; }
        public DbSet<StaticContent> StaticContents { get; set; }
        public DbSet<StaticContentGroup> StaticContentGroups { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            //modelBuilder.Entity<Sys_Admin>().ToTable("Sys_Admins");

            #region SysAdmin tbl

            modelBuilder.Entity<SysAdmin>()
                .HasOptional(p => p.CreateAdmin)
                .WithMany(a => a.CreatedByMe)
                .HasForeignKey(a => a.CreatedBy)
                .WillCascadeOnDelete(false);

            modelBuilder.Entity<SysAdmin>()
                .HasOptional(p => p.UpdateAdmin)
                .WithMany(a => a.UpdatedByMe)
                .HasForeignKey(a => a.UpdatedBy)
                .WillCascadeOnDelete(false);

            modelBuilder.Entity<SysAdmin>()
                .HasMany(r => r.SysRoles)
                .WithMany(a => a.SysAdmins)
                .Map(m =>
                {
                    m.ToTable("SysRolesAdminsMapping");
                    m.MapLeftKey("SysAdminId");
                    m.MapRightKey("SysRoleId");
                });

            //modelBuilder.Entity<SysAdmin>()
            //    .HasMany(a => a.NewsCreatedByMe)
            //    .WithRequired()
            //    .HasForeignKey(a => a.CreatedBy);

            #endregion

            #region SysRole tbl

            modelBuilder.Entity<SysRole>()
                .HasMany(r => r.SysAdmins)
                .WithMany(a => a.SysRoles)
                .Map(m =>
                {
                    m.ToTable("SysRolesAdminsMapping");
                    m.MapLeftKey("SysRoleId");
                    m.MapRightKey("SysAdminId");
                });

            modelBuilder.Entity<SysRole>()
                .HasMany(f => f.SysFeatures)
                .WithMany(r => r.SysRoles)
                .Map(m =>
                {
                    m.ToTable("SysRolesFeaturesMapping");
                    m.MapLeftKey("SysRoleId");
                    m.MapRightKey("SysFeatureId");
                });

            #endregion

            #region SysFeature tbl

            modelBuilder.Entity<SysFeature>()
                .HasMany(r => r.SysRoles)
                .WithMany(f => f.SysFeatures)
                .Map(m =>
                    {
                        m.ToTable("SysRolesFeaturesMapping");
                        m.MapLeftKey("SysFeatureId");
                        m.MapRightKey("SysRoleId");
                    });

            modelBuilder.Entity<SysFeature>()
                .HasOptional(f => f.Parent)
                .WithMany(f => f.Childrens)
                .HasForeignKey(f => f.ParentId)
                .WillCascadeOnDelete(false);

            #endregion

            #region News tbl
            modelBuilder.Entity<News>()
                .HasRequired(n => n.CreatedByAdmin)
                .WithMany(a => a.NewsCreatedByMe)
                .HasForeignKey(n => n.CreatedBy);

            modelBuilder.Entity<News>()
                .HasOptional(n => n.UpdatedByAdmin)
                .WithMany()
                .HasForeignKey(n => n.UpdatedBy);

            modelBuilder.Entity<News>()
                .HasMany(g => g.NewsGroups)
                .WithMany(n => n.News)
                .Map(m =>
                {
                    m.MapLeftKey("NewsId");
                    m.MapRightKey("NewsGroupId");
                    m.ToTable("NewsNewsGroupsMapping");
                });
            #endregion

            #region NewsGroup tbl
            modelBuilder.Entity<NewsGroup>()
                .HasRequired(g => g.CreatedByAdmin)
                .WithMany()
                .HasForeignKey(g => g.CreatedBy);

            modelBuilder.Entity<NewsGroup>()
                .HasOptional(g => g.UpdatedByAdmin)
                .WithMany()
                .HasForeignKey(g => g.UpdatedBy);

            modelBuilder.Entity<NewsGroup>()
                .HasMany(g => g.News)
                .WithMany(n => n.NewsGroups)
                .Map(m =>
                {
                    m.MapLeftKey("NewsGroupId");
                    m.MapRightKey("NewsId");
                    m.ToTable("NewsNewsGroupsMapping");
                });
            #endregion

            #region StaticContentGroup tbl
            modelBuilder.Entity<StaticContentGroup>()
                .HasRequired(g => g.CreatedByAdmin)
                .WithMany()
                .HasForeignKey(g => g.CreatedBy);

            modelBuilder.Entity<StaticContentGroup>()
                .HasOptional(g => g.UpdatedByAdmin)
                .WithMany()
                .HasForeignKey(g => g.UpdatedBy);
            modelBuilder.Entity<StaticContentGroup>()
                .HasMany(g => g.StaticContents)
                .WithRequired(c => c.StaticContentGroup)
                .HasForeignKey(c => c.StaticContentGroupId);
            #endregion

            #region StaticContent tbl
            modelBuilder.Entity<StaticContent>()
                .HasRequired(g => g.CreatedByAdmin)
                .WithMany()
                .HasForeignKey(g => g.CreatedBy);

            modelBuilder.Entity<StaticContent>()
                .HasOptional(g => g.UpdatedByAdmin)
                .WithMany()
                .HasForeignKey(g => g.UpdatedBy);

            modelBuilder.Entity<StaticContent>()
                .HasRequired(g => g.StaticContentGroup)
                .WithMany(g => g.StaticContents)
                .HasForeignKey(g => g.StaticContentGroupId);
            #endregion
        }
    }
}
